package com.frxuexi.apps

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import coil.compose.AsyncImage
import com.frxuexi.apps.intent.NewsIntent
import com.frxuexi.apps.model.NewsState
import com.frxuexi.apps.model.RecordEntity
import com.frxuexi.apps.ui.theme.Demo01_ComposeTheme
import com.frxuexi.apps.viewmodel.NewsViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class AppsActivity : ComponentActivity() {
    lateinit var vm:NewsViewModel
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Demo01_ComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    var records by remember {
                        mutableStateOf(ArrayList<RecordEntity>())
                    }
//                    Greeting("Android")
                    vm=ViewModelProvider(this).get(NewsViewModel::class.java)
                    NewsList(records = records)
                    lifecycleScope.launch {
                        vm.newsState.collect{
                            when(it){
                                is NewsState.onSuccess->{
                                    Log.i("===",it.records.toString())
                                    records= it.records as ArrayList<RecordEntity>
                                }
                                is NewsState.onError->{
                                    Log.i("===",it.msg)
                                }
                                is NewsState.onLoading->{

                                }

                            }
                        }
                    }

                    lifecycleScope.launch {
                        //发送意图
                        vm.newsChannel.send(NewsIntent.getNews(1))
                    }
                }
            }
        }
    }
}

@Composable
fun NewsList(records: List<RecordEntity>){
    LazyColumn(content = {
        items(records){
            Column {
                Text(text = it.title)
                AsyncImage(model = it.imgurl, contentDescription = "", modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp))
            }
        }
    })
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )

}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Demo01_ComposeTheme {
        Greeting("Android")
    }
}